package com.system.exam.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.system.exam.admin.core.customBean.PageBean;
import com.system.exam.admin.core.http.Result;
import com.system.exam.admin.entity.BusCategory;
import com.system.exam.admin.service.BusCategoryService;
import com.system.exam.admin.service.BusExamQuestionService;
import com.system.exam.admin.service.BusQuestionInfoService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/business/category")
@Api("tags=分类表")
public class BusCategoryController {
    @Autowired
    private BusCategoryService busCategoryService;

    @Autowired
    private BusExamQuestionService busExamQuestionService;

    @Autowired
    private BusQuestionInfoService busQuestionInfoService;


    @PostMapping("/list")
    @PreAuthorize("hasAuthority('business:category:list')")
    public Result list(@RequestBody PageBean pageBean) {
        Page<BusCategory> pageResult = busCategoryService.page(
                new Page<>(pageBean.getPageNum(),
                        pageBean.getPageSize()),
                new QueryWrapper<BusCategory>().orderByDesc("sort"));
        List<BusCategory> list = pageResult.getRecords();
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("list", list);
        resultMap.put("total", pageResult.getTotal());
        return Result.success(resultMap);
    }

    @Transactional
    @PostMapping("/save")
    @PreAuthorize("hasAuthority('business:category:add')" + "||" + "hasAuthority('business:category:edit')")
    public Result saveCategory(@RequestBody BusCategory busCategory) {
        if (busCategory.getId() > 0) {
            busCategoryService.updateById(busCategory);
            busQuestionInfoService.updateBusQuestionInfoById(busCategory.getCategoryName(), busCategory.getId());
            busExamQuestionService.updateBusExamQuestionById(busCategory.getCategoryName(), busCategory.getId());
        } else {
            busCategoryService.save(busCategory);
        }
        return Result.success(busCategory);
    }

    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('business:category:query')")
    public Result findById(@PathVariable long id) {
        BusCategory busCategory = busCategoryService.getById(id);
        return Result.success(busCategory);
    }

    @Transactional
    @GetMapping("/delete/{id}")
    @PreAuthorize("hasAuthority('business:category:delete')")
    public Result delete(@PathVariable long id) {
        busCategoryService.removeById(id);
        busQuestionInfoService.deleteBusQuestionInfoById(id);
        busExamQuestionService.deleteBusExamQuestionById(id);
        return Result.success();
    }

    /**
     * 查看分类名称是否重名
     *
     * @param busCategory
     * @return
     */
    @PostMapping("/checkCategoryName")
    @PreAuthorize("hasAuthority('business:category:query')")
    public Result checkCategoryName(@RequestBody BusCategory busCategory) {
        if (busCategoryService.checkCategoryName(busCategory.getCategoryName()) == null) {
            return Result.success();
        } else {
            return Result.error(600, "分类名称已存在");
        }
    }
}
